<?php 
	if(empty($_GET['a'])){
		header("location:index.php");
		exit;
	}
	require "../extends/Model.class.php";
	require "../extends/config.php";
	$model = new Model("s_product");
	switch ($_GET['a']){
		case 'delete':
			 if($model->delete($_GET['pro_id'])>0){
			 	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
			 	echo "<script>alert('删除成功')</script>";
			 	echo "<script>location.href='index.php'</script>";
			 }else{
			 	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
			 	echo "<script>alert('删除失败')</script>";
			 	echo "<script>history.back()'</script>";
			 }
		 break;
		 case 'edit':

			 if($model->save($_POST)>0){
			 	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
			 	echo "<script>alert('修改成功')</script>";
			 	echo "<script>location.href='index.php'</script>";
			 }else{
			 	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
			 	echo "<script>alert('修改失败')</script>";
			 	echo "<script>history.back()'</script>";
			 }
		break;
		case 'add':
			 if($model->add($_POST)>0){
			 	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
			 	echo "<script>alert('添加成功')</script>";
			 	echo "<script>location.href='index.php'</script>";
			 }else{
			 	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
			 	echo "<script>alert('添加失败')</script>";
			 	echo "<script>history.back()'</script>";
			 }
		break;

		case "imagedelete":
			echo "test";
			$imageModel = new Model('s_image');
			//var_dump($imageModel);
			$result = $imageModel->delete($_GET['img_id']);
			//var_dump($result);
			if($result>0){
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "删除成功";
				echo "<script>location.href='index.php'</script>";
				// echo "<meta http-equiv='refresh' content='2,url=index.php?page=1'>";
				// echo "<script>alert('删除成功')</script>";
				// echo "<script>location.href='image_index.php'</script>";
			}else {
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "删除失败";
			}
			break;

		case "addimage":
			require "../extends/Upload.class.php";
			$upload = new Upload();
			$upload->exts = ['jpg','png','jpeg'];
			$upload->rootPath = "../Uploads/";
			$info = $upload->upload();
			// var_dump($upload);
			// var_dump($info);
			if(!$info){
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script>alert('".$upload->getError()."')</script>";
				echo "<script>history.back()</script>";
			}else {
				// 图片上传成功
				$imageModel = new Model("s_image");
				$data['img_path'] = $info['images']['savepath'];
				$data['img_name'] = $info['images']['savename'];
				$data['pro_id'] = $_POST['pro_id'];
				// $data['img_addtime']= date();
				// var_dump($data);
				// var_dump($imageModel->add($data));
				// return;
				 if($imageModel->add($data)>0){
				 	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				 	echo "<script>alert('添加成功')</script>";
			 		 echo "<script>location.href='image_index.php?pro_id=".$_POST['pro_id']."'</script>";
				 }else {					
				 	$filePath = "../Uploads/".$data['img_path'].$data['img_name'];
				 	unlink($filePath);
				 	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				 	echo "<script>alert('添加失败')</script>";
				 	 echo "<script>history.back()</script>";
				 }
			}
			break;
		case 'image_edit':
			$imageModel = new Model("s_image");
		 // if($model->save($_POST)>0){
			 if($imageModel->save($_POST)>0){
			 	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
			 	echo "<script>alert('修改成功')</script>";
			 	echo "<script>location.href='image_index.php?pro_id=".$_POST['pro_id']."'</script>";
			 }else{
			 	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
			 	echo "<script>alert('修改失败')</script>";
			 	echo "<script>location.href='image_index.php?pro_id=".$_POST['pro_id']."'</script>";
			 }
		break;
	}
?>